package com.imooc.product.message;

import com.imooc.product.common.ProductInfoOutput;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author Jiayuan
 * @version 1.0
 * @description:
 * @time 11/8/2018 10:45 AM
 */

@Component
public class ProductInfoReceiver {

    public final String PRODUCT_STOCK_TEMPLATE = "product_stock_%s";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queuesToDeclare = @Queue("productInfo"))
    public void process(String message){
        //message => ProductOutput
        //暂未写
        ProductInfoOutput productInfoOutput =  null;  //(ProductInfoOutput) JsonUtil.fromJSON(message,productInfoOutput.class);
        //redis缓存
        stringRedisTemplate.opsForValue().set(String.format(PRODUCT_STOCK_TEMPLATE,productInfoOutput.getProductId()),
                String.valueOf(productInfoOutput.getProductStock()));

    }


    //补7.8代码 5.40

}
